package com.example.the8

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.the8.weather.Forecast
import com.example.the8.weather.Weather
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class SecondActivity:AppCompatActivity() {
    val baseUrl="http://t.weather.itboy.net/api/weather/city/"
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.layout2)
        val city=findViewById<TextView>(R.id.city)
        val province:TextView=findViewById(R.id.province)
        val wendu:TextView=findViewById(R.id.wendu)
        val shidu:TextView=findViewById(R.id.shidu)
        val imageView:ImageView=findViewById(R.id.imageView)
        val listView:ListView=findViewById(R.id.listView)
       // val cityCode=intent.getBundleExtra("city_code")
        val cityCode=intent.getStringExtra("city_code")
        val queue=Volley.newRequestQueue(this)
        val strRequest=StringRequest(baseUrl+cityCode,{
            val gson=Gson()
            val WeatherType=object :TypeToken<Weather>() {}.type
            val weather:Weather=gson.fromJson<Weather>(it,WeatherType)
            city.text=weather.cityInfo.city
            province.text=weather.cityInfo.parent
            wendu.text=weather.data.wendu
            shidu.text=weather.data.shidu
            val firstDay=weather.data.forecast.first()
            when(firstDay.type){
                "晴" ->imageView.setImageResource(R.drawable.sun)
                "阴" ->imageView.setImageResource(R.drawable.mcloud)
                "多云" ->imageView.setImageResource(R.drawable.cloud)
                "阵雨" ->imageView.setImageResource(R.drawable.rain)
                "下雪" ->imageView.setImageResource(R.drawable.snow)
                else ->imageView.setImageResource(R.drawable.thunder)
            }
            val adapter = ArrayAdapter<Forecast>(this,android.R.layout.simple_list_item_1,weather.data.forecast)
            listView.adapter=adapter

            Log.d("SecondActivity","${weather.cityInfo.city}   ${weather.cityInfo.parent}")
        }, {
            Log.d("SecondActivity","$it")
        } )
        queue.add(strRequest)
    }
}